package com.edu.admin.utils;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;

import com.edu.admin.utils.ReflectionUtil;
import com.edu.admin.common.BaseContextHandler;


/**
 * 实体类相关工具类
 * 
 * @author Wangjiong
 * 
 */
public class EntityUtils {
	/**
	 * @param entity
	 *            
	 */
	public static <T> void setCreatAndUpdatInfo(T entity) {
		setCreateInfo(entity);
		setUpdatedInfo(entity);
	}
	
	/**
	 * @param entity       
	 * 
	 */
	public static <T> void setUpdatInfo(T entity) {
		setUpdatedInfo(entity);
	}

	/**
	 * @param entity
	 *
	 *
	 */
	public static <T> void setCreateInfo(T entity) {

		Long createUserId = BaseContextHandler.getAdminId();
		String[] fields = { "createUserId", "createTime"};
		Field field = ReflectionUtil.getAccessibleField(entity, "createTime");

		Object[] value = null;
		if (field != null && field.getType().equals(Date.class)) {
			value = new Object[] { createUserId, new Date()};

			setDefaultValues(entity, fields, value);
		}

	}

	public static <T> void setId(T entity) {
		try {
			List<Field> fields = ReflectionUtil.getAccessibleFields(entity);
			for (Field field : fields) {
				Id id = field.getAnnotation(Id.class);
				if (id != null && field.getType() == String.class) {
					field.set(entity, IdWorker.getInstance().nextId());
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param entity
	 * 
	 */
	public static <T> void setUpdatedInfo(T entity) {

		Long updateUserId = BaseContextHandler.getAdminId();
		String[] fields = { "updateUserId", "updateTime"};
		Field field = ReflectionUtil.getAccessibleField(entity, "updateTime");

		Object[] value = null;
		if (field != null && field.getType().equals(Date.class)) {
			value = new Object[] { updateUserId, new Date()};

			setDefaultValues(entity, fields, value);
		}

	}
	
	
	/**
	 * 启用设置
	 * @param entity
	 * 
	 *
	 */
	public static <T> void setUse(T entity) {

		// 默认属性
		String[] fields = { "useType" };
		Field field = ReflectionUtil.getAccessibleField(entity, "useType");
		// 默认值
		Object[] value = null;
		if (field != null) {
			value = new Object[] { 0 };
			// 填充默认属性值
			setDefaultValues(entity, fields, value);
		}

	}

	/**
	 * 停用设置
	 *
	 * @param entity
	 *
	 */
	public static <T> void setNotUse(T entity) {

		// 默认属性
		String[] fields = { "useType" };
		Field field = ReflectionUtil.getAccessibleField(entity, "useType");
		// 默认值
		Object[] value = null;
		if (field != null) {
			value = new Object[] { 1 };
			// 填充默认属性值
			setDefaultValues(entity, fields, value);
		}

	}

	/**
	 * 
	 * @param entity
	 * @param fields
	 * @param value
	 *
	 */
	private static <T> void setDefaultValues(T entity, String[] fields, Object[] value) {
		for (int i = 0; i < fields.length; i++) {
			String field = fields[i];
			if (ReflectionUtil.hasField(entity, field)) {
				ReflectionUtil.invokeSetter(entity, field, value[i]);
			}
		}
	}

	/**
	 * 
	 * @param entity
	 * @param field
	 * 
	 * @return
	 */
	public static <T> boolean isPKNotNull(T entity, String field) {
		if (!ReflectionUtil.hasField(entity, field)) {
			return false;
		}
		Object value = ReflectionUtil.getFieldValue(entity, field);
		return value != null && !"".equals(value);
	}
}
